package by.pavlyukevich.paymentsystem.panel.card;

import java.util.Arrays;
import java.util.Date;

import javax.inject.Inject;

import org.apache.wicket.ajax.AjaxRequestTarget;
import org.apache.wicket.ajax.markup.html.form.AjaxSubmitLink;
import org.apache.wicket.bean.validation.PropertyValidator;
import org.apache.wicket.event.Broadcast;
import org.apache.wicket.extensions.markup.html.form.DateTextField;
import org.apache.wicket.markup.html.form.CheckBox;
import org.apache.wicket.markup.html.form.DropDownChoice;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.form.IChoiceRenderer;
import org.apache.wicket.markup.html.form.TextField;
import org.apache.wicket.markup.html.panel.Panel;
import org.apache.wicket.model.CompoundPropertyModel;
import org.apache.wicket.model.PropertyModel;

import by.pavlyukevich.paymentsystem.add.ErrorEvent;
import by.pavlyukevich.paymentsystem.add.PanelUpdateEvent;
import by.pavlyukevich.paymentsystem.datamodel.card.Card;
import by.pavlyukevich.paymentsystem.datamodel.card.CardLimit;
import by.pavlyukevich.paymentsystem.datamodel.card.LimitType;
import by.pavlyukevich.paymentsystem.services.card.CardLimitService;
import by.pavlyukevich.paymentsystem.services.card.CardService;
import by.pavlyukevich.paymentsystem.webapp.BasicAuthenticationSession;

public class CardLimitEditPanel extends Panel {

	@Inject
	CardLimitService cardLimitService;
	@Inject
	CardService cardService;

	public CardLimitEditPanel(String id, CardLimit cardLimit, Panel respPanel) {
		super(id);
		setOutputMarkupId(true);

		Form<CardLimit> form = new Form<CardLimit>("form", new CompoundPropertyModel<CardLimit>(
				cardLimit));
		add(form);

		form.add(new DropDownChoice<Card>("card", new PropertyModel<Card>(cardLimit, "card"),
				cardService.getList(), new IChoiceRenderer<Card>() {
					@Override
					public Object getDisplayValue(Card card) {
						return card.getCardNumber();
					}

					@Override
					public String getIdValue(Card object, int index) {
						return Integer.toString(index);
					}
				}).add(new PropertyValidator<Card>()).setEnabled(
				BasicAuthenticationSession.get().getRoles().contains("ADMIN")));

		form.add(new DateTextField("periodBegin", "dd.MM.yyyy").add(new PropertyValidator<Date>()));
		form.add(new DateTextField("periodEnd", "dd.MM.yyyy").add(new PropertyValidator<Date>()));

		form.add(new DropDownChoice<LimitType>("limitType", new PropertyModel<LimitType>(cardLimit,
				"limitType"), Arrays.asList(LimitType.values()))
				.add(new PropertyValidator<LimitType>()));

		form.add(new TextField<String>("amount").add(new PropertyValidator<String>()));
		form.add(new TextField<String>("quantity").add(new PropertyValidator<String>()));
		form.add(new CheckBox("block").add(new PropertyValidator<String>()));

		form.add(new AjaxSubmitLink("save") {
			@Override
			protected void onSubmit(AjaxRequestTarget target, Form<?> form) {
				cardLimitService.saveOrUpdate(cardLimit);
				send(getPage(), Broadcast.BREADTH, new PanelUpdateEvent(target, respPanel));
			}

			@Override
			protected void onError(AjaxRequestTarget target, Form<?> form) {
				send(getPage(), Broadcast.BREADTH, new ErrorEvent(target));
			}

		});

	}

}
